﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class orderDetailController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static orderDetailInfo getbyID(string orderID)
        {
            orderDetailInfo objOD = new orderDetailInfo();
            using (SqlCommand cmd = new SqlCommand("getIDOrderDetail", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", orderID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objOD.orderID = dr.GetInt32(0);
                        objOD.productID = dr.GetInt32(1);
                        objOD.quantity = dr.GetInt32(2);
                        objOD.price = dr.GetString(3);
                        objOD.status = dr.GetBoolean(4);
                    }
                }
                dr.Close();
            }
            return objOD;
        }

        #endregion

        #region getbyAll
        public static DataTable ListOrderDetail()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listOrderDetail", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertOrderDetail(orderDetailInfo objOrderDetail)
        {
            using (SqlCommand cmd = new SqlCommand("insertOrderDetail", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", objOrderDetail.orderID));
                cmd.Parameters.Add(new SqlParameter("@productID", objOrderDetail.productID));
                cmd.Parameters.Add(new SqlParameter("@quantity", objOrderDetail.quantity));
                cmd.Parameters.Add(new SqlParameter("@price", objOrderDetail.price));
                cmd.Parameters.Add(new SqlParameter("@status", objOrderDetail.status));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateOrderDetail(orderDetailInfo objOrderDetail)
        {
            using (SqlCommand cmd = new SqlCommand("updateOrderDetail", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", objOrderDetail.orderID));
                cmd.Parameters.Add(new SqlParameter("@productID", objOrderDetail.productID));
                cmd.Parameters.Add(new SqlParameter("@quantity", objOrderDetail.quantity));
                cmd.Parameters.Add(new SqlParameter("@price", objOrderDetail.price));
                cmd.Parameters.Add(new SqlParameter("@status", objOrderDetail.status));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteOrderDetail(string orderID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteOrderDetail", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", orderID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
    }
}
